package com.ruoyi.coupons.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.DefaultJackson2JavaTypeMapper;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;



@Configuration
public class RabbitMqConfiguration {

    @Bean
    public MessageConverter jsonMessageConverter() {
        Jackson2JsonMessageConverter converter = new Jackson2JsonMessageConverter();
        converter.setJavaTypeMapper(new DefaultJackson2JavaTypeMapper() {
            {
                // 设置信任的包
                setTrustedPackages("com.ruoyi.coupons.domain");
            }
        });// 信任的包
        return converter;
    }

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory, MessageConverter jsonMessageConverter) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(jsonMessageConverter);
        return rabbitTemplate;
    }
    //注册队列
    @Bean("couponQ")
    public Queue getQueue1(){
        return QueueBuilder.durable("couponQ").build();
    }


    //注册交换机
    @Bean("couponExchange")
    public Exchange getExchange1(){
        return ExchangeBuilder.topicExchange("couponExchange").durable(true).build();
    }

    //绑定
    @Bean("bind")
    public Binding getBind(){
        return BindingBuilder.bind(getQueue1()).to(getExchange1()).with("a").noargs();
    }

}
